package com.example.shipload_e;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.HashMap;

import android.app.Fragment;
import android.app.ProgressDialog;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.SimpleAdapter;
import android.widget.Toast;

import com.example.shipload_e.service.BizNetworkService;
import com.example.shipload_e.util.HttpActConstant;
import com.example.shipload_e.util.SharePreferenceUtil;

public class ContentFragment extends Fragment {
	SharePreferenceUtil	  spu;
	// private static String SHIPMENT_FEEDBACK = "装船反馈";
	private static String	TRANSPORT_FEEDBACK	  = "运输反馈";
	// private static String DISCHARGE_FEEDBACK = "卸船反馈";
	private static String	EXCEPTION_FEEDBACK	  = "异常反馈";
	private static String	SHIP_ARRIVAL	      = "船舶到港";
	private static String	DAILY_DISCHARGE_COUNT	= "每日卸船数";
	private static String	DISCHARGE_FINISH	  = "卸船完成";
	private static String	DAILY_SHIPMENT_COUNT	= "每日装船数";
	private static String	SHIPMENT_FINISH	      = "装船完成";
	private static String	SHIP_LEAVE_WHARF	  = "船舶离港";
	private static String	NEWS_COLLECT	      = "信息汇总";
	MyHandler	          handler;

	ProgressDialog	      progressDialog;

	static class MyHandler extends Handler {
		WeakReference<ContentFragment>	tActivity;

		MyHandler(ContentFragment activity) {
			tActivity = new WeakReference<ContentFragment>(activity);
		}

		@Override
		public void handleMessage(Message msg) {
			ContentFragment theActivity = tActivity.get();
			theActivity.progressDialog.dismiss();
			Bundle data = msg.getData();
			if(!data.isEmpty()) {
				Toast.makeText(theActivity.getActivity(), data.getString("msg"), 400).show();
			} else {}
		}
	};

	@Override
	public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
		GridView gridView;
		View view = inflater.inflate(R.layout.fragment_content, container, false);
		handler = new MyHandler(this);
		progressDialog = new ProgressDialog(getActivity());
		spu = new SharePreferenceUtil(getActivity());
		gridView = (GridView) view.findViewById(R.id.Grid_View);
		// 定义九宫格因素（图标）
		ArrayList<HashMap<String, Object>> meumList = new ArrayList<HashMap<String, Object>>();
		HashMap<String, Object> map1 = new HashMap<String, Object>();
		map1.put("ItemImage", R.drawable.ship_arrival);
		meumList.add(map1);
		HashMap<String, Object> map2 = new HashMap<String, Object>();
		map2.put("ItemImage", R.drawable.daily_shipment_count);
		meumList.add(map2);
		HashMap<String, Object> map3 = new HashMap<String, Object>();
		map3.put("ItemImage", R.drawable.shipment_finish);
		meumList.add(map3);
		HashMap<String, Object> map4 = new HashMap<String, Object>();
		map4.put("ItemImage", R.drawable.ship_leave_wharf);
		meumList.add(map4);
		HashMap<String, Object> map5 = new HashMap<String, Object>();
		map5.put("ItemImage", R.drawable.yun_shu_tap);
		meumList.add(map5);
		HashMap<String, Object> map6 = new HashMap<String, Object>();
		map6.put("ItemImage", R.drawable.ship_arrival);
		meumList.add(map6);
		HashMap<String, Object> map7 = new HashMap<String, Object>();
		map7.put("ItemImage", R.drawable.daily_discharge_count);
		meumList.add(map7);
		HashMap<String, Object> map8 = new HashMap<String, Object>();
		map8.put("ItemImage", R.drawable.discharge_finish);
		meumList.add(map8);
		HashMap<String, Object> map9 = new HashMap<String, Object>();
		map9.put("ItemImage", R.drawable.yi_chang_tap);
		meumList.add(map9);
		HashMap<String, Object> map10 = new HashMap<String, Object>();
		map10.put("ItemImage", R.drawable.ren_wu_hui_zong);
		meumList.add(map10);
		// HashMap<String, Object> map6 = new HashMap<String, Object>();
		// map6.put("ItemImage", R.drawable.xie_chuan_tap);
		// meumList.add(map6);
		SimpleAdapter saItem = new SimpleAdapter(getActivity(), meumList, R.layout.meunitem, new String[]{"ItemImage" }, new int[]{R.id.ItemImage });  // 对应R的Id

		// 添加Item到网格中
		gridView.setAdapter(saItem);
		// 添加点击事件
		/**
		 * 以下页面跳转在BizNetWorkService中进行，获取到的数据直接赋值到下一页面并进行处理
		 */
		gridView.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0,View view,int arg2,long arg3) {
				switch (arg2) {
				// 船舶到港——装船
					case 0:
						spu.setTitleName(SHIP_ARRIVAL);
						// startActivity(new Intent(getActivity(), ShipArrivalActivity.class));
						new Thread(new BizNetworkService(getActivity(), handler, HttpActConstant.LOADING_ARRIVALTASKS, null))
						        .start();

						break;
					// 每日装船数
					case 1:
						// Toast.makeText(getActivity(), DAILY_SHIPMENT_COUNT, Toast.LENGTH_SHORT)
						// .show();
						spu.setTitleName(DAILY_SHIPMENT_COUNT);
						new Thread(new BizNetworkService(getActivity(), handler, HttpActConstant.INIT_DAILY_SHIPMENT_COUNT_TASKS, null))
						        .start();
						// startActivity(new Intent(getActivity(),
						// DailyShipmentCountListActivity.class));
						break;
					// 装船完成
					case 2:
						// Toast.makeText(getActivity(), SHIPMENT_FINISH,
						// Toast.LENGTH_SHORT).show();
						spu.setTitleName(SHIPMENT_FINISH);
						new Thread(new BizNetworkService(getActivity(), handler, HttpActConstant.INIT_SHIPMENT_FINISH_TASKS, null))
						        .start();
						// startActivity(new Intent(getActivity(),
						// ShipmentFinishListActivity.class));
						break;
					// 船舶离港
					case 3:
						// Toast.makeText(getActivity(), SHIP_LEAVE_WHARF,
						// Toast.LENGTH_SHORT).show();
						spu.setTitleName(SHIP_LEAVE_WHARF);
						new Thread(new BizNetworkService(getActivity(), handler, HttpActConstant.INIT_SHIP_LEAVE_TASKS, null))
						        .start();
						// startActivity(new Intent(getActivity(), LeavePortListActivity.class));
						break;
					// 运输反馈
					case 4:
						// Toast.makeText(getActivity(), TRANSPORT_FEEDBACK, Toast.LENGTH_SHORT)
						// .show();
						spu.setTitleName(TRANSPORT_FEEDBACK);
						new Thread(new BizNetworkService(getActivity(), handler, HttpActConstant.INIT_TRANSPORT_FEEDBACK_TASKS, null))
						        .start();
						// startActivity(new Intent(getActivity(),
						// TransportFeedbackListActivity.class));
						break;
					// 船舶到港-卸船
					case 5:
						// Toast.makeText(getActivity(), SHIP_ARRIVAL, Toast.LENGTH_SHORT).show();
						spu.setTitleName(SHIP_ARRIVAL);
						new Thread(new BizNetworkService(getActivity(), handler, HttpActConstant.INIT_DISCHARGE_SHIP_ARRIVAL_TASKS, null))
						        .start();
						// startActivity(new Intent(getActivity(),
						// DischargeShipArrivalActivity.class));
						break;
					// 每日卸船数
					case 6:
						// Toast.makeText(getActivity(), DAILY_DISCHARGE_COUNT, Toast.LENGTH_SHORT)
						// .show();
						spu.setTitleName(DAILY_DISCHARGE_COUNT);
						new Thread(new BizNetworkService(getActivity(), handler, HttpActConstant.INIT_DAILY_DISCHARGE_COUNT_TASKS, null))
						        .start();
						// startActivity(new Intent(getActivity(),
						// DisDailyShipmentCountListActivity.class));
						break;
					// 卸船完成
					case 7:
						// Toast.makeText(getActivity(), DISCHARGE_FINISH,
						// Toast.LENGTH_SHORT).show();
						spu.setTitleName(DISCHARGE_FINISH);
						// startActivity(new Intent(getActivity(),
						// DischargeFinishListActivity.class));
						new Thread(new BizNetworkService(getActivity(), handler, HttpActConstant.INIT_DISCHARGE_FINISH_TASKS, null))
						        .start();
						break;
					// 异常反馈
					case 8:
						spu.setTitleName(EXCEPTION_FEEDBACK);
						new Thread(new BizNetworkService(getActivity(), handler, HttpActConstant.INIT_EXCEPTION_FEEDBACK_TASKS, null))
						        .start();
						break;
					// 任务汇总
					case 9:
						spu.setTitleName(NEWS_COLLECT);
						new Thread(new BizNetworkService(getActivity(), handler, HttpActConstant.INIT_COLLECT_TASKS, null))
						        .start();
						break;
					default:
						break;
				}
				spu.setIsComeFromWelcome(true);
				progressDialog.setMessage("加载中，请稍等");
				progressDialog.show();
			}
		});
		return view;
	}

}
